#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPainter>
#include<QPoint>
#include<QPen>
#include<QPainterPath>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 设置窗口背景色
    setPalette(QPalette(Qt::white));
    // 设置控件是否自动填充背景色或背景图
    setAutoFillBackground(true);
    resize(600,300);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);
    //生成5角形坐标
    qreal dbR = 100;
    const qreal dbPI = 3.1415926;
    qreal dbRad = (dbPI*72)/180;
    QPoint pts[] = {
        QPoint(dbR,0),
        QPoint(dbR * cos(dbRad),-dbR * sin(dbRad)),
        QPoint(dbR * cos(2*dbRad),-dbR * sin(2 * dbRad)),
        QPoint(dbR * cos(3*dbRad),-dbR * sin(3 * dbRad)),
        QPoint(dbR * cos(4*dbRad),-dbR * sin(4 * dbRad))
    };
    // 设置字体
    QFont font;
    font.setPointSize(12);
    font.setBold(true);
    painter.setFont(font);

    // 设置画笔
    QPen pen;
    pen.setWidth(2); // 线宽
    pen.setColor(Qt::blue); // 颜色
    pen.setStyle(Qt::SolidLine); // 线类型
    pen.setCapStyle(Qt::FlatCap); // 端点样式
    pen.setJoinStyle(Qt::BevelJoin); // 线的连接样式
    painter.setPen(pen);
    // 设置画刷
    QBrush brush;
    brush.setColor(Qt::yellow);
    brush.setStyle(Qt::SolidPattern);
    painter.setBrush(brush);
    // 绘制五角星
    QPainterPath path;
    path.moveTo(pts[0]);
    path.lineTo(pts[2]);
    path.lineTo(pts[4]);
    path.lineTo(pts[1]);
    path.lineTo(pts[3]);
    // 闭合路径，最后一个点与第一个点相连
    path.closeSubpath();
    // 显示端点编号
    path.addText(pts[0],font,"0");
    path.addText(pts[1],font,"1");
    path.addText(pts[2],font,"2");
    path.addText(pts[3],font,"3");
    path.addText(pts[4],font,"4");
    // 绘图
    painter.save();
    // 平移
    painter.translate(100,120);
    painter.drawPath(path);
    painter.drawText(0,0,"S1");
    // 恢复坐标
    painter.restore();

    //
    painter.translate(300,120);
    // 缩放
    painter.scale(0.8,0.8);
    // 顺时针旋转90°
    painter.rotate(90);
    painter.drawPath(path);
    painter.drawText(0,0,"S2");

    // 恢复所有坐标变换
    painter.resetTransform();
    painter.translate(500,120);
    painter.rotate(-145);
    painter.drawPath(path);
    painter.drawText(0,0,"S3");

}
